package com.suian.tools;

import java.util.HashMap;
import java.util.Map;

/**
 * @author suian
 * @date 2020/11/13 13:38
 */
public class ByteUtil {
    /**
     * 返回结果为byte类型的异或校验
     *
     * @param datas
     * @return
     */
    public static byte getXor(byte[] datas) {
        byte temp = datas[0];
        for (int i = 1; i < datas.length; i++) {
            temp ^= datas[i];
        }
        return temp;
    }

    /**
     * 返回计算结果为十六进制字符串的异或校验
     *
     * @param str
     * @return
     */
    public static String getXor2String(String str) {
        byte[] bytes = ByteUtil.hexStringToByteArray(str);
        byte xor = getXor(bytes);
        //字节转十六进制
        String s = Integer.toHexString(0xFF & xor);
        return s;
    }
    /**
     * 十进制转十六进制,小于16的十进制前面补一个0
     * @param decNum 字符串类型的十进制数
     * @return String类型十六进制数
     */
    public static String decStringToHexStringDiv(String decNum) {
        int n = Integer.parseInt(decNum.trim(), 10);
        int flag = n;
        StringBuffer s = new StringBuffer();
        String a;
        char[] b = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        while (n != 0) {
            s = s.append(b[n % 16]);
            n = n / 16;
        }
        a = s.reverse().toString();
        if (flag < 16) {
            a = 0 + a;
        }
        return a;
    }
    /**
     * 十进制转十六进制,小于16的十进制前面补一个0
     * @param n int类型十进制数
     */
    public static String decToHexStringDiv(int n) {
        int flag = n;
        StringBuffer s = new StringBuffer();
        String a;
        char[] b = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        while (n != 0) {
            s = s.append(b[n % 16]);
            n = n / 16;
        }
        a = s.reverse().toString();
        if (flag < 16) {
            a = 0 + a;
        }
        return a;
    }

    /**
     * 十进制转十六进制
     *
     * @param n
     * @return
     */
    public static String decToHexString(int n) {
        StringBuffer s = new StringBuffer();
        String a;
        char[] b = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        while (n != 0) {
            s = s.append(b[n % 16]);
            n = n / 16;
        }
        a = s.reverse().toString();
        return a;
    }

    /**
     * 十六进制转十进制
     *
     * @param content 十六进制数
     * @return int 类型
     */
    public static int hexToDecString(String content) {
        return Integer.parseInt(content, 16);
    }

    /**
     * 十六进制转十进制
     * @param content 十六进制
     * @return 字符串(String)类型
     */
    public static String hexStringToDecString(String content) {
        String ret = "";
        try {
            ret= String.valueOf(Integer.parseInt(content, 16));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            ret = "0";
        }
        return ret;
    }

    /**
     * byte[]数组转换为16进制的字符串
     *
     * @param bytes 要转换的字节数组
     * @return 转换后的结果
     */
    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    public static String bytesToHexStringAnd0(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
            sb.append(" ");
        }
        return sb.toString();
    }

    /**
     * 16进制表示的字符串转换为字节数组
     * 返回的字节数组表示为十进制
     *
     * @param hexString 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    public static String decToBinaryString(int decNum) {
        return Integer.toBinaryString(decNum);
    }
}